/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;

import java.util.Date;
import java.util.List;
import org.just.dusin.ooii.util.DateUtil;

/**
 *实时状态刷新线程，定时刷新状态。数据是否延时到达
 * @author Administrator
 */
public class RealFreshThread implements Runnable{
    private boolean isStop = false;
    private Date Date;
    
    @Override
    public void run() {       
        
        while(!isStop){
            DatabaseConnectionUtil dcu = new DatabaseConnectionUtil();
            RealoutDao realDao = new RealoutDao(dcu.getConnection());
            RealDeviceDao realDevice = new RealDeviceDao(dcu.getConnection());
            Date now = new Date();
            try{
                List<RealoutBean> allRealout = realDao.findAll();
                
                for(RealoutBean realout:allRealout){
                    Date previous = DateUtil.get(realout.getTime(), "yyyy-MM-dd HH:mm");
                    Date updatetime = DateUtil.get(realout.getUpdateTime(), "yyyy-MM-dd HH:mm");
                    if(now.getTime()-updatetime.getTime()>1000*60*10){
                        realDao.doUpdateStatus("2", realout.getOrgCode());  //网络不通
                        realDevice.updateWLStatus(1, realout.getOrgCode()); //设备网络不通
                    }
                    else if(now.getTime()-previous.getTime()>1000*60*10){  //超过10分钟更新状态
                        realDao.doUpdateStatus("1", realout.getOrgCode()); //数据延时         
                        realDevice.updateWLStatus(0, realout.getOrgCode());
                    }else{
                        realDao.doUpdateStatus("0", realout.getOrgCode());
                        realDevice.updateWLStatus(0, realout.getOrgCode());
                    }
                }
                
                Thread.sleep(60000);
            }catch(InterruptedException e){
                
            }catch(Exception e){
                
            }finally{
                dcu.close();
            }
        }
    }
    public void stop(){
        isStop = true;
    }
    
}
